package com.xilihui.xlh.business.activitys;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.IntegralEntity;
import com.xilihui.xlh.business.requests.MineRequest;
import com.xilihui.xlh.business.widget.JifengPopWindow;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.TextStringUtils;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/6.
 */

public class IntegralActivity extends BaseCompatActivity implements OnRefreshListener, OnLoadMoreListener {
    
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    
    @BindView(R.id.tv_num)
    TextView tv_num;
    @BindView(R.id.tv_all)
    TextView tv_all;
    
    ArrayList<IntegralEntity.DataBean> data = new ArrayList<>();
    BaseAdapter<IntegralEntity.DataBean> baseAdapter;
    
    int page = 1;
    String type = "10086";
    
    @Override
    public int getLayout() {
        return R.layout.activity_integral;
    }

    @Override
    public String initTitle() {
        return "我的积分";
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
       
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        baseAdapter = new BaseAdapter<IntegralEntity.DataBean>(this,data) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_integral;
            }

            @Override
            protected void getView(BaseViewHolder holder, IntegralEntity.DataBean entity, int position) {
                holder.setText(R.id.tv_title,entity.getDesc());
                holder.setText(R.id.tv_num,entity.getPay_points());
                holder.setText(R.id.tv_time,entity.getChange_time());
            }
        };
        recyclerView.setAdapter(baseAdapter);
        
    }

    @Override
    public void initData() {
        getData(true);
    }
    
    public void getData(boolean isLoading){

        MineRequest.integral(IntegralActivity.this,page,type)
                .compose(DoTransform.<IntegralEntity>applyScheduler(this,isLoading))
                .compose(this.<IntegralEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<IntegralEntity>(this) {
                    @Override
                    protected void onSuccess(IntegralEntity entity) {
                        if(page==1){
                            data.clear();
                            tv_num.setText(entity.getPoints());
                        }
                        
                        data.addAll(entity.getData());
                        baseAdapter.setList(data);
                        
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        
                        if(page>=entity.getPageCount()){
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }else {
                            smartRefreshLayout.setNoMoreData(false);
                        }
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                    }
                });
        
    }
    
    @OnClick(R.id.tv_all)
    public void all(){
        JifengPopWindow popWindow = new JifengPopWindow(this);
        popWindow.showAsDropDown(tv_all,-80,25);
    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        getData(false);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page = page + 1;
        getData(false);
    }

    @Subscribe
    public void onEventMainThread(YEventBuses.Event event) {
        super.onEventMainThread(event);
        if(event.is(JifengPopWindow.JIFENG)){
            type = (String) event.getParams(0);
            if(type.equals("10086")){
                tv_all.setText("全部");
            }else if(type.equals("1")){
                tv_all.setText("获取");
            }else if(type.equals("2")){
                tv_all.setText("使用");
            }
                
            page = 1;
            getData(true);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
